package com.hdoit.kylin.logical.document.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.hdoit.kylin.logical.document.dao.IWeekLogDao;
import com.hdoit.kylin.logical.document.model.WeekLog;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.group.model.Project;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.document.WeekLogSearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
/**
 * @author      lufu
 * @email       
 * @createtime  2017年08月10日 14:46:25
 * @comment
 */
@Component
public class WeekLogDaoImpl extends SuperDao<WeekLog> implements IWeekLogDao{

	private ITeacherStudentService teacherStudentService;
	
	
	public ITeacherStudentService getTeacherStudentService() {
		return teacherStudentService;
	}

	@Resource
	public void setTeacherStudentService(
			ITeacherStudentService teacherStudentService) {
		this.teacherStudentService = teacherStudentService;
	}

	@Override
	public List<WeekLog> queryList(WeekLogSearch weekLogSearch,PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT weekLog.*")
						.from(WeekLog.class, "weekLog")
						.leftJoin(Student.class, "student")
						.on("weekLog.createUserId", "student.employeeId")
						.addOrder("weekLog.createDate",OrderEnum.desc)
						.setMainTableAlias("weekLog");
		
		appendFinder(weekLogSearch,finderExpression);
		
		if (pagerModel!=null) {
			return queryList(finderExpression, weekLogSearch);
		}
		return queryList(finderExpression, weekLogSearch, pagerModel);
	}

	private void appendFinder(WeekLogSearch weekLogSearch,IFinderExpression finder) {
		
		if (!StringUtils.isEmpty(weekLogSearch.getEmployeeId())) {
			finder.andWhere("weekLog.createUserId=#{employeeId}")
				  .setParam("employeeId", weekLogSearch.getEmployeeId());
		}
	
	}

	@Override
	public List<WeekLog> queryList(WeekLogSearch weekLogSearch) {
		return queryList(weekLogSearch,null);
	}

	@Override
	public int queryCount(WeekLogSearch weekLogSearch) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT weekLog.id) as count")
						.leftJoin(Student.class, "student")
						.on("weekLog.createUserId", "student.employeeId")
						.setMainTableAlias("weekLog");
		
		appendFinder(weekLogSearch,finderExpression);
		
		return queryCount(finderExpression, weekLogSearch);
	}

	@Override
	public List<WeekLog> queryListByTeacherId(String teacherId) {
		TeacherStudentSearch tSsearch = new TeacherStudentSearch();
		tSsearch.setGraduateMark(YesOrNoInteger.no);
		tSsearch.setTeacherId(teacherId);
		List<TeacherStudent> list = getTeacherStudentService().queryList(tSsearch,null);
		
		List<WeekLog> weekLogs = new ArrayList<WeekLog>();
		for(TeacherStudent temp:list){
			WeekLogSearch search = new WeekLogSearch();
			search.setEmployeeId(temp.getStudentId());
			List<WeekLog> tempList = queryList(search,null);
			for(WeekLog tempLog:tempList){
				weekLogs.add(tempLog);
			}
		}
		return weekLogs;
	}

	@Override
	public List<WeekLog> queryListByEmployeeId(String employeeId) {
		WeekLogSearch search = new WeekLogSearch();
		search.setEmployeeId(employeeId);
		return queryList(search,null);
	}

}